
namespace SharpTalk.Client.WpfApplication
{
    using System;
    using System.Windows.Media.Imaging;

    /// <summary>
    /// Miscellaneous helper methods
    /// </summary>
    public static class Helpers
    {
        /// <summary>
        /// Loads an embedded Image from the assembly
        /// </summary>
        /// <param name="name">The name of the image file, with the extension, located in the Images directory.</param>
        /// <returns>A BitmapImage or Null</returns>
        public static BitmapImage LoadImage(string name)
        {
            try
            {
                var bi = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri(String.Format("pack://application:,,/Images/{0}", name), UriKind.Absolute);
                bi.EndInit();
                return bi;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Loads an image from a file
        /// </summary>
        /// <param name="file">The complet file path to the image</param>
        /// <param name="maximumSize"></param>
        /// <returns>A bitmapImage or null</returns>
        public static BitmapImage LoadImageFromFile(string file, int maximumSize)
        {
            try
            {
                var bi = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri(file, UriKind.RelativeOrAbsolute);
                bi.EndInit();
                
                if(bi.Width > maximumSize)
                    bi.DecodePixelWidth = maximumSize;

                return bi;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}